/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.uch.syscom.hibernateSpringDao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import pe.edu.uch.syscom.mapping.TbVenta;
import pe.edu.uch.syscom.mapping.TbVentaDetalle;
import pe.edu.uch.syscom.utils.ConstantesWeb;

/**
 *
 * @author Jose Tejada
 */
public class HSVentaDAOImpl extends HibernateDaoSupport implements HSVentaDAO {

    @Override
    public int cambiarEstadoVenta( TbVenta tbVenta, String sNuevoEstado ) {
        int iRes;
        String sQuery;
        SQLQuery sqlQuery;

        iRes = 0;
        if ( tbVenta != null ) {
            sQuery = "UPDATE tb_venta SET estado=? WHERE id_venta=? AND activo=?";
            sqlQuery = this.getSession().createSQLQuery( sQuery );
            sqlQuery.setString( 0, sNuevoEstado );
            sqlQuery.setInteger( 1, tbVenta.getIdVenta() );
            sqlQuery.setInteger( 2, 1 );
            iRes = sqlQuery.executeUpdate();
        }
        return iRes;
    }

    @Override
    public void guardarVenta( TbVenta tbVenta ) {

        if ( tbVenta != null ) {
            this.getHibernateTemplate().saveOrUpdate( tbVenta );
        }
    }

    @Override
    public void guardarVentaDetalle( TbVentaDetalle tbVentDet ) {
        if ( tbVentDet != null ) {
            this.getHibernateTemplate().save( tbVentDet );
        }
    }

    @Override
    public List<TbVenta> listarVentasPorEstadoYFecha( String sEstadoVenta, Calendar calFechDesde, Calendar calFechHasta ) {
        Criteria criteria;
        List<TbVenta> lstVentas;

        try {
            calFechHasta.add( Calendar.DATE, 1 );
            criteria = this.getSession().createCriteria( TbVenta.class );
            criteria.add( Restrictions.eq( "activo", 1 ) ).
                    add( Restrictions.between( "fecha", calFechDesde.getTime(), calFechHasta.getTime() ) ).
                    add( Restrictions.eq( "estado", sEstadoVenta ) );

            lstVentas = criteria.list();
        } catch ( Exception ex ) {
            lstVentas = new ArrayList<TbVenta>();
            ex.printStackTrace();
        }
        return lstVentas;
    }

    @Override
    public TbVenta traerVentaPorId( int iIdVenta ) {
        Criteria criteria;
        TbVenta tbVenta;

        try {
            criteria = this.getSession().createCriteria( TbVenta.class );
            tbVenta = (TbVenta) criteria.add( Restrictions.eq( "idVenta", iIdVenta ) ).add( Restrictions.eq( "activo", 1 ) ).uniqueResult();
        } catch ( Exception ex ) {
            ex.printStackTrace();
            tbVenta = null;
        }

        return tbVenta;
    }

    @Override
    public void actualizarVentDet( TbVentaDetalle tbVentaDetAux ) {
        try {
            this.getHibernateTemplate().saveOrUpdate( tbVentaDetAux );
        } catch ( Exception ex ) {
            ex.printStackTrace();
        }
    }

    @Override
    public int efectuarPago( TbVenta tbVenta ) {
        int iRes;
        iRes = ConstantesWeb.ERROR;
        return iRes;
    }
}
